<template>
  <div class="data-table">
    <el-table
      ref="multipleTable"
      v-loading="loading"
      :data="tableData"
      style="width: 100%"
      :height="height"
      :row-key="getRowKeys"
      class="hc-table"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        v-if="isShow"
        type="selection"
        width="55"
        fixed="left"
        align="center"
      />
      <template v-for="(col, index) in columns">
        <!-- 操作列/自定义列 -->
        <slot
          v-if="col.slot"
          :name="col.slot"
        />
        <!-- 普通列 -->
        <el-table-column
          v-else
          :key="index"
          :prop="col.prop"
          :label="col.label"
          :width="col.width"
          :formatter="col.formatter"
          align="center"
        />
      </template>
    </el-table>
    <!-- 是否调用分页 -->
    <el-pagination
      v-if="paginate && paginate.total > 0"
      class="pagination-block"
      :current-page="paginate.currentPage"
      :page-sizes="[10, 20, 30, 40, 50, 100]"
      :page-size="paginate.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="paginate.total"
      background
      @size-change="paginate.handleSizeChange"
      @current-change="paginate.handleCurrentChange"
    />
  </div>
</template>

<script>
export default {
  name: 'DataTable',
  props: {
    // eslint-disable-next-line vue/require-default-prop
    height: {
      type: Number
    },
    tableData: {
      type: Array,
      default: () => {
        return []
      }
    },
    columns: {
      type: Array,
      default: () => {
        return []
      }
    },
    loading: {
      type: Boolean,
      default: false
    },
    paginate: {
      type: Object,
      default: () => {
        return {}
      }
    },
    isShow: {
      type: Boolean,
      default: true
    },
    // eslint-disable-next-line vue/require-default-prop,vue/require-prop-types
    getRowKeys(row) {
      row.id
    }
  },
  methods: {
    handleSelectionChange(val) {
      this.$emit('handleFunc', val)
    },
    toggleRowSelection(row) {
      this.$refs.multipleTable.toggleRowSelection(row)
    },
    toggleRowSelection2(row) {
      this.$refs.multipleTable.toggleRowSelection(row, true)
    },
    clearSelection() {
      this.$refs.multipleTable.clearSelection()
    }
  }
}
</script>

<style lang="scss">
.hc-table {
  th {
    background: #f7f7f7;
  }

  td {
    padding: 15px 0 !important;
    font-size: 13px;
  }
}

</style>
